package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysDangerUnit;
import com.ruoyi.system.domain.SysMonitorPoint;
import com.ruoyi.system.domain.SysPlan;
import com.ruoyi.system.domain.vo.DangerCheckDeptVO;
import com.ruoyi.system.mapper.SysDangerUnitMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysMonitorPointMapper;
import com.ruoyi.system.mapper.SysPlanMapper;
import com.ruoyi.system.service.ISysDangerUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysDangerUnitServiceImpl implements ISysDangerUnitService {

    @Autowired
    private SysDangerUnitMapper sysDangerUnitMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SysPlanMapper sysPlanMapper;

    @Autowired
    private SysMonitorPointMapper sysMonitorPointMapper;

    @Override
    public void create(SysDangerUnit sysDangerUnit) {
        sysDangerUnitMapper.create(sysDangerUnit);
    }

    @Override
    public List<SysDangerUnit> list(SysDangerUnit sysDangerUnit) {
        return sysDangerUnitMapper.list(sysDangerUnit);
    }

    @Override
    public void delete(Integer id) {
        sysDangerUnitMapper.delete(id);
    }

    @Override
    public void update(SysDangerUnit sysDangerUnit) {
        sysDangerUnitMapper.update(sysDangerUnit);
    }


    @Override
    public List<DangerCheckDeptVO> getAllDept() {
        List<SysDangerUnit> list = sysDangerUnitMapper.list(new SysDangerUnit());
        List<DangerCheckDeptVO> dangerCheckDeptVOS = new ArrayList<>();
        Map<String,List<String>> map = new HashMap<>();
        Map<Integer,String> map1 = new HashMap<>();
        list.forEach(i -> {
            Integer deptId = i.getDeptId();
            String deptName = map1.get(deptId);
            if (StringUtils.isEmpty(deptName)) {
                deptName = i.getDeptName();
                map1.put(deptId,deptName);
            }
            List<String> strings = map.get(deptName);
            if (CollectionUtils.isEmpty(strings)) {
                strings = new ArrayList<>();
                map.put(deptName,strings);
            }
            strings.add(i.getUnitName());
        });
        map1.keySet().forEach(id -> {
            DangerCheckDeptVO dangerCheckDeptVO = new DangerCheckDeptVO();
            String deptName = map1.get(id);
            List<String> list1 = map.get(deptName);
            dangerCheckDeptVO.setDeptId(id);
            dangerCheckDeptVO.setDeptName(deptName);
            dangerCheckDeptVO.setDangerName(list1);
            dangerCheckDeptVOS.add(dangerCheckDeptVO);
        });
        return dangerCheckDeptVOS;
    }

    @Override
    public String importExcel(List<SysDangerUnit> sysDangerUnits, boolean updateSupport, String operName) {
        if (StringUtils.isNull(sysDangerUnits) || sysDangerUnits.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        for (SysDangerUnit sysDangerUnit : sysDangerUnits) {
            // 公司
            String deptParentName = sysDangerUnit.getDeptParentName();
            if (StringUtils.isNotEmpty(deptParentName)) {
                List<SysDept> sysDepts = sysDeptMapper.selectByDeptName(deptParentName);
                if (!CollectionUtils.isEmpty(sysDepts)) {
                    SysDept sysDept = sysDepts.get(0);
                    sysDangerUnit.setDeptParentId(sysDept.getDeptId().intValue());
                }
            }
            // 部门
            String deptName = sysDangerUnit.getDeptName();
            if (StringUtils.isNotEmpty(deptName)) {
                List<SysDept> sysDepts = sysDeptMapper.selectByDeptName(deptName);
                if (!CollectionUtils.isEmpty(sysDepts)) {
                    SysDept sysDept = sysDepts.get(0);
                    sysDangerUnit.setDeptId(sysDept.getDeptId().intValue());
                }
            }
            // 处置方案
            String planName = sysDangerUnit.getPlanName();
            if (StringUtils.isNotEmpty(planName)) {
                SysPlan byName = sysPlanMapper.getByName(planName);
                if (byName != null) {
                    sysDangerUnit.setPlanId(byName.getId().toString());
                }
            }
            // 监控点
            String monitorPointName = sysDangerUnit.getMonitorPointName();
            if (StringUtils.isNotEmpty(monitorPointName)) {
                SysMonitorPoint sysMonitorPoint = sysMonitorPointMapper.getByName(monitorPointName);
                if (sysMonitorPoint != null) {
                    sysDangerUnit.setMonitorPointId(sysMonitorPoint.getId().toString());
                }
            }
        }
        sysDangerUnitMapper.insertBatch(sysDangerUnits);
        return "成功";
    }
}
